package guoanmaker.personal.business.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import guoanmaker.personal.business.model.PersonalUser;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.modelnobind.PersonalUserForUser;
import guoanmaker.personal.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.personal.business.service.PersonalCodeService;
import guoanmaker.personal.business.service.PersonalInvoiceService;
import guoanmaker.personal.business.service.PersonalOrderFormService;
import guoanmaker.personal.business.service.PersonalUserService;
import guoanmaker.personal.helper.common.AjaxJson;
import guoanmaker.personal.helper.common.abstractobj.ApiController;
import guoanmaker.personal.helper.common.util.SystemHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;
/**
 * 
* @ClassName: ServeAppController 
* @Description: 提供app的controller
* @author Yvonne_MGL
* @date 2017年7月6日 下午3:45:04 
*  
 */
@RestController
@RequestMapping(ApiController.APP_URL)
public class ServeAppController {

	@Resource
	private PersonalUserService personalUserService;

	@Resource
	private PersonalInvoiceService personalInvoiceService;
	
	@Resource
	private PersonalCodeService personalCodeService;
	
	
	@Autowired  
	private  HttpServletRequest request;
	
	@Resource
	private PersonalOrderFormService personalOrderFormService;
	
	/**
	 * 
	* @Description: 注册用户
	* @param username 用户名,即手机号
	* @param password 密码
	* @param code 验证码
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:39:03     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "注册接口", notes = "注册")
	@RequestMapping(value = "/registered", method = RequestMethod.POST)
	public AjaxJson registeredUser(String username, String password,String code,String recommendid,String company,String department) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		try {
			returnResult=personalUserService.registeredUser(username, password,code,"2",recommendid,company,department);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
		}
		
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 验证用户名密码 
	* @param username 用户名
	* @param password 密码
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:39:49     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "验证接口", notes = "验证用户名密码")
	@RequestMapping(value = "/verify", method = RequestMethod.POST)
	public AjaxJson verify(String username, String password) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		try {
			String sessionid=request.getSession().getId();
			Jedis jedis=new Jedis("123.56.48.63",6379); 
			jedis.auth("218007");
			jedis.set(sessionid, "1");
			String ss=jedis.get(username);
			if(ss==null){
				jedis.set(username, sessionid+",");
			}else{
				jedis.set(username, ss+sessionid+",");
			}
			jedis.disconnect();
			returnResult=personalUserService.verify(username, password,"2");
			returnResult.setSessionid(sessionid);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
		}
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 修改密码,重置密码
	* @param username 用户名
	* @param password 密码
	* @param code 验证码
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:40:19     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "修改密码接口", notes = "修改密码")
	@RequestMapping(value = "/changePassword", method = RequestMethod.POST)
	public AjaxJson changePassword(String username, String password,String code) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		try {
			returnResult=personalUserService.changePassword(username, password,code);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
		}
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 修改个人资料
	* @param userid 用户id
	* @param portraitFile  头像图片
	* @param nickname 昵称
	* @param birthday 生日
	* @param name 姓名
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:40:58     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "修改资料接口", notes = "修改用户资料")
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ApiImplicitParam(paramType = "form", name = "portraitFile", dataType = "File")
	public AjaxJson updateUser(String userid, @RequestParam(required = false) MultipartFile portraitFile, String nickname,
			String birthday,String name,String company,String department,String stage,String bent) {
		AjaxJson ajaxJson=new AjaxJson();
		PersonalUserForUser personalUser=new PersonalUserForUser();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(personalUser);
		}else{
			try {
				personalUser=personalUserService.updateUser(userid, portraitFile, nickname, birthday,name,company,department,stage,bent);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(personalUser);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(personalUser);
			}
		}
		
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 查询用户资料 
	* @param userid 用户id
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:41:50     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询用户接口", notes = "查询用户资料")
	@RequestMapping(value = "/findById", method = RequestMethod.POST)
	public AjaxJson findById(String userid) {
		AjaxJson ajaxJson=new AjaxJson();
		PersonalUserForUser personalUser=new PersonalUserForUser();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(personalUser);
		}else{
			try {
				personalUser=personalUserService.findById(userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(personalUser);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(personalUser);
			}
		}
		
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 保存用户发票
	* @param userid 用户id
	* @param invoiceName 发票名称
	* @param taxNumber 发票税号
	* @param consignee 收货人
	* @param telephone 电话
	* @param region 地区
	* @param address 地址
	* @param zipCode 邮编
	* @param isdefault 是否为默认收货地址,1为是,0为否
	* @date 2017年7月6日 下午3:35:17     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "保存发票接口", notes = "保存发票")
	@RequestMapping(value = "/saveInvoice", method = RequestMethod.POST)
	public AjaxJson saveInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,@RequestParam(required = false)MultipartFile businessLicenseFile,@RequestParam(required = false)MultipartFile businessFile,String isdefault,String user_id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.saveInvoice(invoiceName, taxNumber, invoiceType, telephone, address, bankName, bankNumber, businessLicenseFile, businessFile, isdefault, user_id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 删除发票
	* @param id 发票id
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:37:11     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "删除发票接口", notes = "删除发票")
	@RequestMapping(value = "/deleteInvoice", method = RequestMethod.POST)
	public AjaxJson deleteInvoice(String id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.deleteInvoice(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param id 发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午2:03:57     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "修改发票接口", notes = "修改发票")
	@RequestMapping(value = "/updateInvoice", method = RequestMethod.POST)
	public AjaxJson updateInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,@RequestParam(required = false)MultipartFile businessLicenseFile,@RequestParam(required = false)MultipartFile businessFile,String isdefault,String id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.updateInvoice(invoiceName, taxNumber, invoiceType, telephone, address, bankName, bankNumber, businessLicenseFile, businessFile, isdefault, id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 发送验证码
	* @param telephone 电话
	* @param isuser 判断用户存在发验证码传1,用户不存在发验证码传2,存不存在都发传0
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:42:18     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "发送验证码接口", notes = "发送验证码")
	@RequestMapping(value = "/sendVerificationCode", method = RequestMethod.POST)
	public AjaxJson sendVerificationCode(String telephone,String isuser) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		try {
			returnResult=personalUserService.sendVerificationCode(telephone,isuser);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 手机快捷登陆
	* @param username 用户名
	* @param code 验证码
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:43:31     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "手机快捷登陆接口", notes = "手机快捷登陆")
	@RequestMapping(value = "/fastLanding", method = RequestMethod.POST)
	public AjaxJson fastLanding(String username,String code,String recommendid){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		try {
			String sessionid=request.getSession().getId();
			Jedis jedis=new Jedis("123.56.48.63",6379); 
			jedis.auth("218007");
			jedis.set(sessionid, "1");
			String ss=jedis.get(username);
			if(ss==null){
				jedis.set(username, sessionid+",");
			}else{
				jedis.set(username, ss+sessionid+",");
			}
			jedis.disconnect();
			returnResult=personalUserService.fastLanding(username, code,"2",recommendid);
			returnResult.setSessionid(sessionid);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			e.printStackTrace();
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 注销用户 
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:44:00     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "注销接口", notes = "注销")
	@RequestMapping(value = "/signOut", method = RequestMethod.POST)
	public AjaxJson signOut(){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			String sessionid=request.getSession().getId();
			String sessionid1=request.getHeader("Authorization");
			if(sessionid1!=null){
				sessionid=sessionid1.substring(7,sessionid1.length());
			}
			Jedis jedis=new Jedis("123.56.48.63",6379); 
			jedis.auth("218007");
			jedis.del(sessionid);
			jedis.disconnect();
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(null);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description:对比验证码是否一致
	* @param username 手机号
	* @param code 验证码
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:44:19     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "验证验证码接口", notes = "验证验证码")
	@RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
	public AjaxJson verifyCode(String username,String code){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		try {
			returnResult=personalCodeService.verifyCode(username, code);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建一口价商品订单 
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:09:09     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建一口价商品订单", notes = "创建一口价商品订单")
	@RequestMapping(value = "/createNotBargainingOrder", method = RequestMethod.POST)
	public AjaxJson createNotBargainingOrder(String userid,String commodityid,String contactPerson,String contactNumber,String requirementDescription,String serverPlace,String rangeid){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createNotBargainingOrder(userid, commodityid, contactPerson, contactNumber, requirementDescription,"2",serverPlace,rangeid);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建议价商品订单 
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:14:23     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建议价商品订单", notes = "创建议价商品订单")
	@RequestMapping(value = "/createBargainingOrder", method = RequestMethod.POST)
	public AjaxJson createBargainingOrder(String userid,String commodityid,String contactPerson,String contactNumber,String requirementDescription,String serverPlace){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createBargainingOrder(userid, commodityid, contactPerson, contactNumber, requirementDescription,"2",serverPlace);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建工位租用订单
	* @param userid 用户id
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param days 租用天数
	* @param quantity 租用数量
	* @param remark 备注
	* @param placeOrderType 下单类型
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月23日 下午2:38:02     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建工位租用订单", notes = "创建工位租用订单")
	@RequestMapping(value = "/createSeatLeaseOrder", method = RequestMethod.POST)
	public AjaxJson createSeatLeaseOrder(String userid,String commodityid,String contactPerson,String contactNumber,String beginDate,String endDate,int quantity,String requirementDescription,String placeOrderType,String serverPlace){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createSeatLeaseOrder(userid, commodityid, contactPerson, contactNumber, beginDate,endDate, quantity, requirementDescription, "2",serverPlace);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建咨询订单 
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param enterpriseName 企业名称
	* @param position 职位
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月15日 上午9:57:20     
	* @return ReturnResult 
	 */
	@ApiOperation(value = "创建咨询订单", notes = "创建咨询订单")
	@RequestMapping(value = "/createAdvisoryOrder", method = RequestMethod.POST)
	public AjaxJson createAdvisoryOrder(String userid,String commodityid,String contactPerson,String contactNumber,
			String requirementDescription,String serverPlace,String enterpriseName,String position){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createAdvisoryOrder(userid, commodityid, contactPerson, contactNumber, requirementDescription,"2",serverPlace,enterpriseName,position);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建咨询订单 
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param enterpriseName 企业名称
	* @param position 职位
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月15日 上午9:57:20     
	* @return ReturnResult 
	 */
	@ApiOperation(value = "创建咨询订单", notes = "创建咨询订单")
	@RequestMapping(value = "/createAdvisoryOrderFixedPrice", method = RequestMethod.POST)
	public AjaxJson createAdvisoryOrderFixedPrice(String userid,String commodityid,String contactPerson,String contactNumber,
			String requirementDescription,String serverPlace,String enterpriseName,String position){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createAdvisoryOrderFixedPrice(userid, commodityid, contactPerson, contactNumber, requirementDescription,"2",serverPlace,enterpriseName,position);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	/**
	 * 
	* @Description: 根据用户id和订单状态查询订单 
	* @param orderStatus 订单状态
	* @param userid 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:45:19     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询用户订单", notes = "查询用户订单")
	@RequestMapping(value = "/findByProperties", method = RequestMethod.POST)
	public AjaxJson findByProperties(String orderStatus, String userid) {
		AjaxJson ajaxJson=new AjaxJson();
		List<ProvideOrderFormForOrder> orders=new ArrayList<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(orders);
		}else{
			try {
				orders=personalOrderFormService.findByProperties(orderStatus, userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(orders);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(orders);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 根据id获取订单详情 
	* @param id 订单id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:54:51     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询订单详情", notes = "查询订单详情")
	@RequestMapping(value = "/findByid", method = RequestMethod.POST)
	public AjaxJson findByid(String id){
		AjaxJson ajaxJson=new AjaxJson();
		ProvideOrderFormForOrder order=new ProvideOrderFormForOrder();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(order);
		}else{
			try {
				order=personalOrderFormService.findByid(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(order);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(order);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 修改订单为完成支付状态
	* @param id 订单id
	* @param defrayNumber 支付单号
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月13日 上午10:15:38     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "完成付款", notes = "完成付款")
	@RequestMapping(value = "/overPay", method = RequestMethod.POST)
	public AjaxJson overPay(String id,String defrayNumber){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.overPay(id, defrayNumber);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 确认完成订单 
	* @param id 订单id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月13日 上午11:21:07     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "确认完成订单", notes = "确认完成订单")
	@RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
	public AjaxJson confirmOrder(String id){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.confirmOrder(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 用户发起退款 
	* @param id 订单id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月13日 上午11:36:59     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "用户发起退款", notes = "用户发起退款")
	@RequestMapping(value = "/initiateRefunds", method = RequestMethod.POST)
	public AjaxJson initiateRefunds(String id,Double amount,String refundsReason){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.initiateRefunds(id,amount,refundsReason);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: app端保存支付单号 
	* @param orderid 订单id
	* @param defrayNumber 支付单号
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月21日 下午2:19:31     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "用户保存支付单号", notes = "用户保存支付单号")
	@RequestMapping(value = "/setDefrayNumber", method = RequestMethod.POST)
	public AjaxJson setDefrayNumber(String orderid,String defrayNumber,String channel,String payType){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		if (payType!=null && payType.equals("wxgzPAY")) {
			try {
				returnResult=personalOrderFormService.setDefrayNumber(orderid, defrayNumber,channel);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}else{
			String sessionid=request.getSession().getId();
			String sessionid1=request.getHeader("Authorization");
			if(sessionid1!=null){
				sessionid=sessionid1.substring(7,sessionid1.length());
			}
			Jedis jedis=new Jedis("123.56.48.63",6379);
			jedis.auth("218007");
			String value=jedis.get(sessionid);
			jedis.disconnect();
			if(value==null||!value.equals("1")){
				ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
			}else{
				try {
					returnResult=personalOrderFormService.setDefrayNumber(orderid, defrayNumber,channel);
					ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
				} catch (Exception e) {
					ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
				}
			}

		}

		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建一口价商品的秒杀订单 
	* @param userid 用户id
	* @param spikeid 秒杀id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param placeOrderType 下单渠道
	* @param serverPlace 服务区域
	* @param rangeid 
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月22日 下午4:16:15     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建一口价商品秒杀订单", notes = "创建一口价商品订单")
	@RequestMapping(value = "/createSpikeNotBargainingOrder", method = RequestMethod.POST)
	public AjaxJson createSpikeNotBargainingOrder(String userid,String spikeid,String contactPerson,String contactNumber,String requirementDescription,String serverPlace,String rangeid){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createSpikeNotBargainingOrder(userid, spikeid, contactPerson, contactNumber, requirementDescription,"2",serverPlace,rangeid);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建工位租用秒杀订单 
	* @param userid 用户id
	* @param spikeid 秒杀id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param beginDate 租用起始时间
	* @param endDate 租用结束时间
	* @param quantity 数量
	* @param @param requirementDescription
	* @param @param placeOrderType
	* @param @param serverPlace
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月22日 下午4:17:51     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建工位租用秒杀订单", notes = "创建工位租用订单")
	@RequestMapping(value = "/createSpikeSeatLeaseOrder", method = RequestMethod.POST)
	public AjaxJson createSpikeSeatLeaseOrder(String userid,String spikeid,String contactPerson,String contactNumber,String beginDate,String endDate,int quantity,String requirementDescription,String placeOrderType,String serverPlace){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createSpikeSeatLeaseOrder(userid, spikeid, contactPerson, contactNumber, beginDate,endDate, quantity, requirementDescription, "2",serverPlace);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}

}
